<?php
/* This file is part of Mirasol CMS
   (C) 2011-2012 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/core.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/db.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/samples.php";

if (isset ($install))
{
  $connection = db_open ();
  
  /* Create 'users' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_users)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_users)." (id INT NOT NULL auto_increment, fullname VARCHAR (50), username VARCHAR (20), email VARCHAR (50), passwd VARCHAR (35), salt INT, rights INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
  /* See if there is an 'admin' user */
  $result = mysql_query ("SELECT id FROM ".db_maketablename ($table_users)." WHERE username LIKE 'admin'");
  $createadmin = mysql_num_rows ($result) == 0 ? 1 : 0;
  mysql_free_result ($result);
  
  /* Create 'userconfig' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_userconfig)." LIMIT 0,1");
  if (empty ($check))
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_userconfig)." (id INT NOT NULL auto_increment, valuekey VARCHAR (50), value TEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
      /* Upgrade 'config' from version 0.7.0 */
      $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_settings)." LIMIT 0,1");
      if (!empty ($check))
        {
          $result = mysql_query ("SELECT * FROM ".db_maketablename ($table_settings));
          if (mysql_num_rows ($result) > 0)
            {
              $row = mysql_fetch_array ($result);
              $upgrade_sitename = mysql_real_escape_string ($row['sitename']);
              mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('sitename', '$upgrade_sitename')");
              $upgrade_domainname = mysql_real_escape_string ($row['domainname']);
              mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('domainname', '$upgrade_domainname')");
              $upgrade_protocol = mysql_real_escape_string ($row['protocol']);
              mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('protocol', '$upgrade_protocol')");
              mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('use_datasheets', '0')");
            }
          mysql_free_result ($result);
          
          mysql_query ("DROP TABLE ".db_maketablename ($table_settings));
        }
      
      /* A new install? */
      if (!isset ($upgrade_sitename) && !isset ($upgrade_domainname) && !isset ($upgrade_protocol))
        {
          mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('sitename', 'A Mirasol CMS powered website')");;
          mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('domainname', 'www.mydomain.com')");;
          mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('protocol', 'http')");
          mysql_query ("INSERT INTO ".db_maketablename ($table_userconfig)." (valuekey, value) VALUES ('use_datasheets', '0')");
        }
    }

  /* Create 'templates' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_templates)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_templates)." (id INT NOT NULL auto_increment, name VARCHAR(50), fields TEXT, xslt LONGTEXT, php LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      mysql_query ("INSERT INTO ".db_maketablename ($table_templates)." (name, fields, xslt, php) VALUES ('Frontpage', '".mysql_real_escape_string ($sample_fields1)."', '".mysql_real_escape_string ($sample_layout1)."', '')");
      $templ_id1 = mysql_insert_id ($connection);
      mysql_query ("INSERT INTO ".db_maketablename ($table_templates)." (name, fields, xslt, php) VALUES ('About', '".mysql_real_escape_string ($sample_fields2)."', '".mysql_real_escape_string ($sample_layout2)."', '')");
      $templ_id2 = mysql_insert_id ($connection);
    }
  
  /* Create 'sublayouts' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_sublayouts)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_sublayouts)." (id INT NOT NULL auto_increment, name VARCHAR(50), xslt LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      mysql_query ("INSERT INTO ".db_maketablename ($table_sublayouts)." (name, xslt) VALUES ('Poweredby', '".mysql_real_escape_string ($sample_sublayout)."')");
    }
  
  /* Create 'pages' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    {
      mysql_query ("CREATE TABLE ".db_maketablename ($table_pages)." (id INT NOT NULL auto_increment, path VARCHAR (255), version INT, starttime INT, endtime INT, template INT, xmlfeed LONGTEXT, guid VARCHAR (40), lastedit INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
      $now = time ();
      mysql_query ("INSERT INTO ".db_maketablename ($table_pages)." (path, version, starttime, endtime, template, xmlfeed, lastedit) VALUES ('[root]', '1', '".time ()."', '0', '$templ_id1', '".mysql_real_escape_string ($sample_page1)."', '$now')");
      $now++;
      mysql_query ("INSERT INTO ".db_maketablename ($table_pages)." (path, version, starttime, endtime, template, xmlfeed, lastedit) VALUES ('About', '1', '".time ()."', '0', '$templ_id2', '".mysql_real_escape_string ($sample_page2)."', '$now')");
    }
  
  /* Upgrade 'pages' from version 0.6 */
  $check = mysql_query ("SELECT guid FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    mysql_query ("ALTER TABLE ".db_maketablename ($table_pages)." ADD COLUMN guid VARCHAR (40)");
  $check = mysql_query ("SELECT lastedit FROM ".db_maketablename ($table_pages)." LIMIT 0,1");
  if (!$check)
    mysql_query ("ALTER TABLE ".db_maketablename ($table_pages)." ADD COLUMN lastedit INT");

  /* Create 'pages_review' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages_review)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_pages_review)." (id INT NOT NULL auto_increment, path VARCHAR (255), version INT, starttime INT, endtime INT, template INT, xmlfeed LONGTEXT, guid VARCHAR (40), lastedit INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
  
  /* Upgrade 'pages_review' from version 0.6 */
  $result = mysql_query ("SELECT * FROM ".db_maketablename ($table_pages)." WHERE guid IS NULL OR guid=''");
  if (mysql_num_rows ($result) > 0)
    {
      $now = time ();
      while ($row = mysql_fetch_array ($result))
        {
          $guid = createguid ();
          mysql_query ("UPDATE ".db_maketablename ($table_pages)." SET guid='$guid', lastedit='$now' WHERE id='{$row['id']}'");
          if (!mysql_query ("INSERT INTO ".db_maketablename ($table_pages_review)." (path, version, starttime, endtime, template, xmlfeed, guid, lastedit) VALUES ('{$row['path']}', '{$row['version']}', '{$row['starttime']}', '{$row['endtime']}', '{$row['template']}', '".mysql_real_escape_string ($row['xmlfeed'])."', '$guid', '$now')"))
            die (mysql_error ($connection));
          $now++;
        }
    }
  mysql_free_result ($result);
  
  /* Create 'styles' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_styles)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_styles)." (id INT NOT NULL auto_increment, name VARCHAR (50), css LONGTEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles)." (name, css) VALUES ('default', '".mysql_real_escape_string ($sample_style)."')");
    $style_id = mysql_insert_id ($connection);
  }
  
  /* Create 'styles_rel' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_styles_rel)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_styles_rel)." (id INT NOT NULL auto_increment, style_id int, template_id INT, identifier VARCHAR (20), linked BOOL, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles_rel)." (style_id, template_id, identifier, linked) VALUES ('$style_id', '$templ_id1', 'defaultstyle', '1')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_styles_rel)." (style_id, template_id, identifier, linked) VALUES ('$style_id', '$templ_id2', 'defaultstyle', '0')");
  }
  
  /* Create 'files' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_files)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_files)." (id INT NOT NULL auto_increment, filename VARCHAR (100), filename_orig VARCHAR (100), name VARCHAR (50), comment TEXT, doctype INT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_files)." (filename, filename_orig, name, doctype) VALUES ('mirasol_icon.png', 'mirasol_icon.png', 'Mirasol CMS icon', '".array_search ("Images", $doctypes)."')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_files)." (filename, filename_orig, name, doctype) VALUES ('poweredby.png', 'poweredby.png', 'Powered by Mirasol CMS', '".array_search ("Images", $doctypes)."')");
    $file_id = mysql_insert_id ($connection);
  }

  /* Create 'files_rel' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_files_rel)." LIMIT 0,1");
  if (!$check)
  {
    mysql_query ("CREATE TABLE ".db_maketablename ($table_files_rel)." (id INT NOT NULL auto_increment, file_id int, template_id INT, identifier VARCHAR (20), PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());
    mysql_query ("INSERT INTO ".db_maketablename ($table_files_rel)." (file_id, template_id, identifier) VALUES ('$file_id', '$templ_id1', 'img_powered')");
    mysql_query ("INSERT INTO ".db_maketablename ($table_files_rel)." (file_id, template_id, identifier) VALUES ('$file_id', '$templ_id2', 'img_powered')");
  }

  /* Create 'datasheets_def' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_datasheets_def)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_datasheets_def)." (id INT NOT NULL auto_increment, name VARCHAR(50), fields TEXT, filename VARCHAR(100), PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());

  /* Create 'datasheets_rel' table */
  $check = mysql_query ("SELECT * FROM ".db_maketablename ($table_datasheets_rel)." LIMIT 0,1");
  if (!$check)
    mysql_query ("CREATE TABLE ".db_maketablename ($table_datasheets_rel)." (id INT NOT NULL auto_increment, datasheet_id INT, template_id INT, identifier VARCHAR (20), order_field VARCHAR(100), order_asc BOOL, filter TEXT, PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_general_ci") or die (mysql_error ());

  /* Close connection to database */
  db_close ($connection);
}
?>
